/**
 * @(#) SubjectCategoryWiseStudentListPDFView.java
 * Copyright (c) 2011 EdRP, Dayalbagh Educational Institute.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL ETRG OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors: Members of EdRP, Dayalbagh Educational Institute
 */

package in.ac.dei.edrp.cms.controller.reportgeneration;

import in.ac.dei.edrp.cms.dao.reportgeneration.SubjectCategoryWiseStudentListDAO;
import in.ac.dei.edrp.cms.domain.reportgeneration.SubjectWiseMeritList;
import java.awt.Color;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.view.document.AbstractPdfView;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfWriter;

public class SubjectCategoryWiseStudentListPDFView extends AbstractPdfView {
	
	private SubjectCategoryWiseStudentListDAO subjectCategoryWiseStudentListDao;	
		
	public void setSubjectCategoryWiseStudentListDAO(SubjectCategoryWiseStudentListDAO subjectCategoryWiseStudentListDao) {
		this.subjectCategoryWiseStudentListDao = subjectCategoryWiseStudentListDao;
	}

	
	@SuppressWarnings("unchecked")
	protected void buildPdfDocument(Map model,Document document,PdfWriter writer,
			HttpServletRequest request,HttpServletResponse response) throws Exception
	{
		System.out.println("inside pdf view controller");
				
		List <SubjectWiseMeritList> courseGroupList;
		List<SubjectWiseMeritList> studentList=new ArrayList<SubjectWiseMeritList>();
		List<SubjectWiseMeritList> categoryTotalList=new ArrayList<SubjectWiseMeritList>();
		List<SubjectWiseMeritList> completeStudentList=new ArrayList<SubjectWiseMeritList>();
		Table studentTable = null;
		Cell c1;
		
		writer = PdfWriter.getInstance(document,new FileOutputStream("E:\\Subject-Category-Wise Student Merit List"+".pdf"));
		document.setPageSize(PageSize.A4.rotate());
		document.open();
		
		String headerText1 = "NUMBER OF STUDENTS, REGISTRATION BASED (STUDENT-WISE & CATEGORY-WISE):"+ request.getParameter("sessionStartYear")+"-"+request.getParameter("sessionEndYear");
		Paragraph header1 = new Paragraph(headerText1,FontFactory.getFont(FontFactory.HELVETICA,8, Font.NORMAL, new Color(0, 0, 255)));		
		header1.setAlignment(Element.ALIGN_CENTER);
		document.add(header1);
		
		Font cellFont = new Font(Font.HELVETICA, 8);
		
		courseGroupList = (List <SubjectWiseMeritList>) model.get("courseGroupList");
		
		SubjectWiseMeritList subjectWiseMeritList=new SubjectWiseMeritList();
		
		HttpSession session=request.getSession(true);
		subjectWiseMeritList.setEntityId(session.getAttribute("userId").toString().substring(1, 9));
		subjectWiseMeritList.setUniversityId(session.getAttribute("universityId").toString());
		subjectWiseMeritList.setSemesterStartDate(session.getAttribute("semesterStartDate").toString());
		subjectWiseMeritList.setSemesterEndDate(session.getAttribute("semesterEndDate").toString());
		subjectWiseMeritList.setProgramId(request.getParameter("programId"));
		subjectWiseMeritList.setBranchId(request.getParameter("branchId"));
		subjectWiseMeritList.setSpecializationId(request.getParameter("specializationId"));
		subjectWiseMeritList.setSemesterCode(request.getParameter("semesterCode"));
		
		for(int i=0;i<courseGroupList.size();i++){
			subjectWiseMeritList.setCourseGroup(courseGroupList.get(i).getCourseGroup()+"%");
			studentList=subjectCategoryWiseStudentListDao.getStudentListCategoryGenderWise(subjectWiseMeritList);
			completeStudentList.addAll(studentList);
			
			categoryTotalList=subjectCategoryWiseStudentListDao.getStudentListCategoryWise(subjectWiseMeritList);
			
			SubjectWiseMeritList entityObject=subjectCategoryWiseStudentListDao.getEntityInfo(subjectWiseMeritList);
						
			if(i==0){
				studentTable = new Table((studentList.size()+categoryTotalList.size()+4),3);
				
//				studentTable.setPadding(1);
//				studentTable.setSpacing(1);
				studentTable.setBorderWidth(1);
				
				c1 = new Cell(new Phrase(" F-CL-BR-S ",cellFont));		
				c1.setHeader(true);
				c1.setRowspan(2);
				c1.setColspan(3);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" Deptt. ",cellFont));
//				c1.setHorizontalAlignment( Element.ALIGN_RIGHT);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				c1.setRowspan(2);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" Males \n ",cellFont));
				c1.setColspan((studentList.size()/2));
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				c1 = new Cell(new Phrase(" Female ",cellFont));
				c1.setColspan((studentList.size()/2));
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);		
				
				c1 = new Cell(new Phrase(" TOTAL ",cellFont));
				c1.setColspan(categoryTotalList.size());
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				for(int j=0;j<studentList.size();j++){
					c1 = new Cell(new Phrase(studentList.get(j).getCategory(),cellFont));
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					c1.setUseBorderPadding(true);
					studentTable.addCell(c1); 
				}
				
				for(int j=0;j<categoryTotalList.size();j++){
					c1 = new Cell(new Phrase(categoryTotalList.get(j).getCategory(),cellFont));
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_TOP);
					studentTable.addCell(c1);
				}
				
				
				c1 = new Cell(new Phrase(entityObject.getEntityCode()+"-"+request.getParameter("programCode")+"-"+request.getParameter("branchId")+"-"+request.getParameter("specializationId"),cellFont));
				c1.setColspan(3);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				
				c1 = new Cell(new Phrase(courseGroupList.get(i).getCourseGroup(),cellFont));
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				
				for(int j=0;j<studentList.size();j++){
					c1 = new Cell(new Phrase(studentList.get(j).getTotal(),cellFont));					
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1); 
				}
				
				for(int j=0;j<categoryTotalList.size();j++){
					c1 = new Cell(new Phrase(categoryTotalList.get(j).getTotal(),cellFont));
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
				}
			}
			else{
				
				c1 = new Cell(new Phrase(entityObject.getEntityCode()+"-"+request.getParameter("programCode")+"-"+request.getParameter("branchId")+"-"+request.getParameter("specializationId"),cellFont));
				c1.setColspan(3);
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				
				c1 = new Cell(new Phrase(courseGroupList.get(i).getCourseGroup(),cellFont));
				c1.setHorizontalAlignment(Element.ALIGN_CENTER);
				c1.setVerticalAlignment(Element.ALIGN_CENTER);
				studentTable.addCell(c1);
				
				for(int j=0;j<studentList.size();j++){
					c1 = new Cell(new Phrase(studentList.get(j).getTotal(),cellFont));
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1); 
				}
				
				for(int j=0;j<categoryTotalList.size();j++){
					c1 = new Cell(new Phrase(categoryTotalList.get(j).getTotal(),cellFont));
					c1.setHorizontalAlignment(Element.ALIGN_CENTER);
					c1.setVerticalAlignment(Element.ALIGN_CENTER);
					studentTable.addCell(c1);
				}
			}
			
		}	    
			
			studentTable.setBorderWidth(0);		
			studentTable.setBorder(Rectangle.NO_BORDER);
			studentTable.endHeaders();		
			document.add(studentTable);
			document.close();		
	}
	
}

